package com.levi.controller;

import com.levi.common.LeaveEnum;
import com.levi.domain.Leave;
import com.levi.service.FlowableService;
import com.levi.service.LeaveService;
import com.levi.vo.LeaveVO;
import com.levi.vo.TaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shentong
 * @date 2021/8/16 12:34
 * @Description TODO
 */
@Slf4j
@Api(tags = "请假-接口")
@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    private LeaveService leaveService;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Resource
    private ProcessEngine processEngine;

    /**
     * 创建申请，默认创建人是   王五（代码写死的）
     * @param leaveVO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("创建申请")
    public Leave add(@RequestBody @Validated LeaveVO leaveVO) {
        Leave leave = new Leave();
        BeanUtils.copyProperties(leaveVO,leave);
        leave.setStatus(LeaveEnum.TO_BE_COMMIT.getCode());
        leave.setCreateId("1");
        return leaveService.add(leave);
    }


    @PutMapping("/submit/{id}")
    @ApiOperation("提交申请")
    public String submit(@PathVariable("id") String id) {
        leaveService.submit(id);
        return "请假提交完成";
    }



    @PutMapping("/review")
    @ApiOperation("审批")
    public String review(@ApiParam("请假单id") @RequestParam String id,
                       @ApiParam("审批结果") @RequestParam Integer result,
                       @ApiParam("审批意见") @RequestParam String remark) {
        leaveService.review(id,result,remark);
        return "审批完成";

    }

    @GetMapping("/getApplyingTask")
    @ApiOperation("待办任务列表")
    public List<TaskVO> getApplyingTask(@ApiParam("当前登录人") @RequestParam String userName) {
        return flowableService.getApplyingTask(userName);
    }

    @GetMapping("/getApplyedTasks")
    @ApiOperation("已办任务列表")
    public List<TaskVO> getApplyedTasks(@ApiParam("当前登录人") @RequestParam String userName) {
        return flowableService.getApplyedTasks(userName);
    }

    @ApiOperation("流程图片")
    @GetMapping(value = "/image/{processInstanceId}")
    public void image(@PathVariable String processInstanceId, HttpServletResponse response) {
        try {
            byte[] b = flowableService.createImage(processInstanceId);
            response.setHeader("Content-type", "text/xml;charset=UTF-8");
            response.getOutputStream().write(b);
        } catch (Exception e) {
            log.error("ApiFormDetailReource-image:" + e);
            e.printStackTrace();
        }
    }
}
